import React, { Component } from 'react'

export default class APP extends Component {
  constructor(props) {
    super(props)

    this.state = {
      counter: 0
    }
  }

  render() {
    return (
      <div>
        <h2>Current count: {this.state.counter}</h2>
        <button onClick={ e => this.increment() }>+1</button>
        <button id="btn">-1</button>
      </div>
    )
  }

  componentDidMount() {
    // 情况二：原生DOM事件
    document.getElementById('btn').addEventListener('click', () => {
      this.setState({
        counter: this.state.counter - 1
      })
      console.log(this.state.counter)
    })
  }

  increment() {
    // 情况一：将setState放入到定时器中
    setTimeout(() => {
      this.setState({
        counter: this.state.counter + 1
      })
      console.log(this.state.counter)
    }, 0)
  }
}
